package org.fujene.structure;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Arrays;

import org.fujene.toolbox.ByteConverter;
import org.fujene.toolbox.ContentHash;

public class ExtendTerm{
	public String term;
	public int content_types;
	public LookupPointer next;
	public LookupPointer prev;
	public LookupPointer ptr;
	public LookupPointer[] invIndex;
	public long[] firstIDs;
	public int skipptr_index;
	public boolean needSkipPtr;
	public long termHash;
	public int[] termfreq;
	public int docfreq;
	public float termwgt;

	public ExtendTerm(String term, LookupPointer next, LookupPointer prev, LookupPointer ptr,
			int skipptr_index, LookupPointer[] invIndex, long[] firstIDs, int docfreq){
		this.term = term;
		this.termHash = ContentHash.getTermHash(term);
		this.next = next;
		this.prev = prev;
		this.ptr = ptr;
		this.skipptr_index = skipptr_index;

		this.invIndex = invIndex;
		this.firstIDs = firstIDs;
		this.content_types = invIndex.length;
		this.docfreq = docfreq;
	}

	public ExtendTerm(String term, LookupPointer ptr, LookupPointer prev, LookupPointer next,
			int skipPtrIndex, boolean addSkipPtr){
		this.term = term;
		this.ptr = ptr;
		this.prev = prev;
		this.next = next;
		this.skipptr_index = skipPtrIndex;
		this.needSkipPtr = addSkipPtr;
	}

	public ExtendTerm(byte[] termb){
		this.ptr = ByteConverter.getPtr(termb, 0);
		this.next = ByteConverter.getPtr(termb, 6);
		this.skipptr_index = ByteConverter.byteToInt(termb, 12, 3);
		this.docfreq = ByteConverter.byteToInt(termb, 15, 3);

		int invlen = ByteConverter.byteToInt(termb, 18, 2);
		this.invIndex = new LookupPointer[invlen];
		this.firstIDs = new long[invlen];
		for(int i = 0; i < invlen; i++) {
			this.invIndex[i] = ByteConverter.getPtr(termb, 20 + (14 * i));
			this.firstIDs[i] = ByteConverter.byteToLong(termb, 20 + (14 * i) + 6, 8);
		}
		try{
			this.term = new String(Arrays.copyOfRange(termb, 20 + (14 * invlen), termb.length), "UTF-8");
		}catch(UnsupportedEncodingException e){}
	}

	// バイト列を作ります
	public byte[] createByteArray(){
		ByteArrayOutputStream stream = new ByteArrayOutputStream();

		try{
			if(ptr != null)
				stream.write(ByteConverter.setPtr(ptr));
			else stream.write(ByteConverter.setPtr(Constants.EMPTY_PTR));
			if(next != null)
				stream.write(ByteConverter.setPtr(next));
			else stream.write(ByteConverter.setPtr(Constants.EMPTY_PTR));
			stream.write(ByteConverter.intToByte(skipptr_index, 3));
			stream.write(ByteConverter.intToByte(docfreq, 3));
			stream.write(ByteConverter.intToByte(invIndex.length, 2));
			for(int i = 0; i < invIndex.length; i++){
				if(invIndex[i] != null) {
					stream.write(ByteConverter.setPtr(invIndex[i]));
					stream.write(ByteConverter.longToByte(firstIDs[i], 8));
				}else {
					stream.write(ByteConverter.setPtr(Constants.EMPTY_PTR));
					stream.write(ByteConverter.longToByte(-1, 8));
				}
			}
			byte[] termb = term.getBytes("UTF-8");
			stream.write(termb);
		}catch(IOException e){}

		return stream.toByteArray();
	}
}
